<?php

namespace app\home\model;

use app\home\service\LimitAmount;
use Think\Db;
use think\Model;

class Order extends Model
{
    protected $connection = 'db_config2';
    protected $table = 'tradelog';

    public function checkOrder($orderid, $total_price)
    {
        $field = '
                 id,
                 userid,
                 goods_id,
                 amount,
                 status,
                 channel_id,
                 extra
                 ';
        try {
            $order_info = self::field($field)->where('id', $orderid)->find();
            if (empty($order_info)) {
                throw new \Exception('订单不存在');
            }

            if ($order_info['status'] == 1) {
                throw new \Exception('订单状态已经处理了');
            }

            if ($order_info['amount'] != $total_price) {
                throw new \Exception('订单金额不一致' . ' 传参:' . $total_price . ' 实际:' . $order_info['amount']);
            }

            return $order_info->toArray();
        } catch (\Exception $e) {
            mylog('订单异常:'.$e->getMessage());
            return false;
        }
    }

    public function createOrder($userid, $channel_id, $GoodsId, $tradetype = 1, $extra)
    {
        $Dbname = Db::connect('db_config2');
        $OrderId = get_microtimes();

        /*验证用户合法性*/
        checkout_user_validity($userid);
        checkout_order_validity($userid, date('Y-m-d H:i:s', time()));

        // 充值限额处理
        $limitAmount = new LimitAmount($userid);
        $isCanRecharge = $limitAmount->checkIsCanRecharge();
        if ($isCanRecharge != 1) {
            puterror(['msg' => $isCanRecharge]);
        }

        $arr = $Dbname->table('accounts_info')->where('UserID', $userid)->field('SpreaderID,RegisterDate,LoginServer')->find();
        $good_configure_info = getServerGoods($arr['LoginServer'], $GoodsId);
        $data = [
            'id' => $OrderId,
            'userid' => $userid,
            'goods_id' => $GoodsId,
            'amount' => $good_configure_info['Price'],
            'status' => 0, //默认是0,表示未付款
            'channel_id' => $channel_id,
            'SpreaderID' => $arr['SpreaderID'],
            'tradetype' => $tradetype,
            'extra' => $extra,
            'createdat' => date('Y-m-d H:i:s', time()),
            'registerat' => $arr['RegisterDate'],
            'loginIp'=>get_client_ip()
        ];
        $res = $Dbname->table('tradelog')->insert($data);
        if ($res) {
            $return['status'] = '200';
            $return['msg'] = '成功';
            $return['orderid'] = $OrderId;
            $return['price'] = $good_configure_info['Price'];
            $return['subject'] = $good_configure_info['Title'];
            $return['registerat'] = $data['registerat'];
            return json_encode($return);
        } else {
            puterror('订单生成失败');
        }
    }


    public function checkOrderNew($orderid, $total_price)
    {
        try {
            $order_info = self::get(['id'=>$orderid]);

            if (empty($order_info)) {
                return ['code'=>-1,'msg'=>'订单不存在'];
            }

            if($order_info['goods_id'] != 7056) {
                if ($order_info['amount'] != $total_price) {
                    return ['code' => -2, 'msg' => '订单金额不一致' . ' 传参:' . $total_price . ' 实际:' . $order_info['amount']];
                }
            }

            return $order_info->toArray();
        } catch (\Exception $e) {
            return ['code'=>-4,'msg'=>$e->getMessage()];
        }
    }

}